package cn.tedu.account.service;

import cn.tedu.account.entity.Account;
import cn.tedu.account.mapper.AccountMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
@Service
public class AccountSereviceImpl
        implements AccountService{
    @Autowired
    private AccountMapper accountMapper;

    @Transactional   //控制本地事务
    @Override
    public void decrease(Long userId, BigDecimal money) {
        Account a = accountMapper.selectByUserId(userId);
        /*
        a.compareTo()
        返回值
          正数,表示a大
          负数,表示a小
          相同表示0
         */
        if (a.getResidue().compareTo(money) < 0){
            throw new RuntimeException("可用金额不足");
        }
        accountMapper.decrease(userId,money);
    }
}
